package leetcode.剑指offer.后40;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author :songyaozhang
 * @date :Created 2021/7/31 16:58
 * @description :https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/
 */
public class 剑指_45_把数组排成最小的数 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.minNumber(new int[]{824,938,1399,5607,6973,5703,9609,4398,8247}));
        System.out.println("121".compareTo("12"));
    }

    class Solution {
        public String minNumber(int[] nums) {
            String[] strings = new String[nums.length];
            for (int i = 0; i < strings.length; i++) {
                strings[i] = nums[i] + "";
            }
            Arrays.sort(strings, (o1, o2) -> {
                int i = 0, j = 0;
                while (i < o1.length() || j < o2.length()) {
                    if (o1.charAt(i >= o1.length() ? 0 : i)
                            != o2.charAt(j >= o2.length() ? 0 : j))
                        return o1.charAt(i >= o1.length() ? 0 : i)
                                - o2.charAt(j >= o2.length() ? 0 : j);
                    i++;
                    j++;
                }
                String A, B;
                if (o1.length() > o2.length()) {
                    A = o1;
                    B = o2;
                } else {
                    A = o2;
                    B = o1;
                }
                return 0;
            });
            System.out.println(Arrays.toString(strings));
            StringBuilder builder = new StringBuilder();
            for (String string : strings) {
                builder.append(string);
            }
            System.out.println(Arrays.toString(strings));
            return builder.toString();
        }
    }
}
